<?php

namespace App\Http\Controllers\Miniprograme;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class AppCodeController extends ComController
{
    //    获取A码
    public function appCodeA(string $name,string $path='uploads/appCode'){
        $response =  $this->miniProgram->app_code->get('path/to/page');
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs(public_path($path), $name);
        }
        return $filename;
    }
    //    获取B码

    /*
     * @parame $scene string 小程序访问路径
     * @parame @optional array 二维码所需要的参数
     * @parame @name string 二维码名称
     */
    public function appCodeB($scene,$optional = [],$name=null){
        $response =  $this->miniProgram->app_code->getUnlimit($scene, $optional);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $path = 'storage/appCode';
            if($name){
                $filename = $response->saveAs(public_path($path), $name);
            }else{
                $filename = $response->save(public_path($path));
            }
        }
        return $path.'/'.$filename;
    }
    //    小程序二维码
    public function appCodeC(string $name,string $path='uploads/appCode'){
        $response =  $this->miniProgram->app_code->getQrCode('/path/to/page');
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs(public_path($path), $name);
        }
        return $filename;
    }
}
